package com.example.mr5.bluetoothppt.Util;/**
 * Created by Mr.5
 * Date：2018/1/24.
 * Time：22:35
 */

import android.bluetooth.BluetoothSocket;
import android.util.Log;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by Mr.5 
 * Date：2018/1/24.
 * Time：22:35
 */
public class SocketUtil {
    private static BluetoothSocket btSocket = null;
    private OutputStream outStream = null;
    private static String TAG = "SocketUtil";

    public static void setBtSocket(BluetoothSocket socket){
        btSocket = socket;
    }

    public static BluetoothSocket getBtSocket(){
        return btSocket;
    }

    public static boolean connect(){
        try {
            btSocket.connect();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "connect fail:"+e);
            return false;
        }
    }

    public static boolean close(){
        try {
            btSocket.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "close fail:" + e);
            return false;
        }
    }

    public static boolean write(byte[] msgBuffer){
        try {
            btSocket.getOutputStream().write(msgBuffer);
            btSocket.getOutputStream().flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

}
